/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socafeclient;

import entity.Comanda;
import entity.Mesas;
import entity.Usuarios;
import java.util.Iterator;
import java.util.List;
import java.util.TimerTask;
import javax.swing.JOptionPane;

/**
 *
 * @author alberto
 */
public class NuevaCuenta extends javax.swing.JFrame {

    public List meseros;
    public List<Mesas> mesas;
    private Comanda comandaCreada;
    private Mesas mesaSelecionada;
    private Usuarios usuarioSelecionado;
    Usuarios usuario;
    Mesas mesa;
    public boolean status = Boolean.TRUE;
    private java.util.Timer clock = new java.util.Timer();

    /**
     * Creates new form NuevaCuenta
     */
    public NuevaCuenta() {
        meseros = Main.usuariosFacade.findAll();
        mesas = Main.mesasFacade.findAll();
        //jCMesero.addItem("Juan");      

        initComponents();

        for (Iterator it = meseros.iterator(); it.hasNext();) {
            usuario = (Usuarios) it.next();
            if (usuario.getIdrol().getIdrol() == 2) {
                jCMesero.addItem(usuario.getNombre());
            }

        }

        for (Iterator it = mesas.iterator(); it.hasNext();) {
            mesa = (Mesas) it.next();
            if (mesa.getEstatus().equals(Boolean.FALSE)) {
                jCMesa.addItem(mesa.getIdmesa());
            }
        }
                


    }

    public Comanda getComandaCreada() {
        return this.comandaCreada;
    }

    public Mesas getMesaSelecionada() {

        return this.mesaSelecionada;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jCMesero = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jCMesa = new javax.swing.JComboBox();

        jTextField1.setEnabled(false);
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jCMesero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCMeseroActionPerformed(evt);
            }
        });

        jLabel4.setText("Mesa:");

        jToggleButton1.setText("Aceptar");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Mesero:");

        jLabel2.setText("Cuenta:");

        jLabel1.setText("Fecha/Hora  23/12/2011  13:13");

        jCMesa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCMesaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jCMesero, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jCMesa, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jLabel1))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(32, 32, 32)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jCMesero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jCMesa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jToggleButton1)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        // TODO add your handling code here:



        usuario = Main.usuariosFacade.findUsuarioByName(jCMesero.getSelectedItem().toString());
        mesa = Main.mesasFacade.find(jCMesa.getSelectedItem());
        mesa.setEstatus(Boolean.TRUE);
        
        System.out.println("id usuario"+usuario.getIdusuario().toString());    
        System.out.println("mesa"+mesa.getIdmesa());
              

        Main.mesasFacade.edit(mesa);
        Main.comandaFacade.crearComanda(usuario.getIdusuario(),Boolean.TRUE,(Integer)jCMesa.getSelectedItem());
        
        int idComanda=Main.comandaFacade.getLastInsert();
        System.out.println("Comanda creada id ="+ idComanda);
        
        comandaCreada = Main.comandaFacade.find(idComanda);        
        
        JOptionPane.showMessageDialog(this, "LA COMANDA : "+idComanda+ "  SE  CREO EXITOSAMENTE");
            
        ProductosView productos = new ProductosView(comandaCreada,status);   
        productos.setVisible(true);
        this.setVisible(false);
        this.dispose();




        



}//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jCMeseroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCMeseroActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCMeseroActionPerformed

    private void jCMesaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCMesaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCMesaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NuevaCuenta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NuevaCuenta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NuevaCuenta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NuevaCuenta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NuevaCuenta().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jCMesa;
    private javax.swing.JComboBox jCMesero;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    // End of variables declaration//GEN-END:variables
}
